#include <iostream>
#include <pthread.h>
void* threadFunction(void* arg)
//是线程执行的函数，arg 参数用于传递给线程的数据。
 {
    int threadId = *(int*)arg;//解引用
    std::cout << "Thread ID: " << threadId << " is running" << std::endl;
    pthread_exit(NULL);
    //函数用于终止当前线程。
}
int main() {
    pthread_t threads[5];
    int threadIds[5];

    // 创建线程
    for (int i = 0; i < 5; ++i) {
        threadIds[i] = i;
        //函数用于创建一个新线程。
        if (pthread_create(&threads[i], NULL, threadFunction, (void*)&threadIds[i]) != 0) {
            std::cerr << "Error creating thread " << i << std::endl;
            return 1;
        }
    }

    // 等待所有线程完成
    for (int i = 0; i < 5; ++i) {
        if (pthread_join(threads[i], NULL) != 0)
        //函数用于等待线程结束。
         {
            std::cerr << "Error joining thread " << i << std::endl;
            return 1;
        }
    }

    std::cout << "All threads have finished execution" << std::endl;
    return 0;
}



